unit frm_AbonentTraitSet;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, shbl_Card, PropFilerEh, PropStorageEh, ActnList, ComCtrls,
  ToolWin, StdCtrls, Buttons, ExtCtrls, DB, FIBDatabase, pFIBDatabase,
  FIBDataSet, pFIBDataSet, DBGridEh, Mask, DBCtrlsEh, DBLookupEh;

type
  TfrmAbonentTraitSet = class(TshblCard)
    dsTrait: TpFIBDataSet;
    trLocal: TpFIBTransaction;
    dscTrait: TDataSource;
    dsTraitValues: TpFIBDataSet;
    dscTraitValues: TDataSource;
    trList: TpFIBTransaction;
    dsTraitList: TpFIBDataSet;
    dscTraitList: TDataSource;
    lcbTraitName: TDBLookupComboboxEh;
    Label1: TLabel;
    dsTraitListID: TFIBIntegerField;
    dsTraitListNAME: TFIBStringField;
    dsTraitListCODE: TFIBStringField;
    dsTraitListLIST_TRAIT: TFIBIntegerField;
    dsTraitValuesID: TFIBIntegerField;
    dsTraitValuesNAME: TFIBStringField;
    dsTraitID: TFIBIntegerField;
    dsTraitABONENT_ID: TFIBIntegerField;
    dsTraitTRAITS_ID: TFIBIntegerField;
    dsTraitTRAITS_VALUE_ID: TFIBIntegerField;
    dsTraitTRAITS_VALUE: TFIBStringField;
    dsTraitBDATE: TFIBDateField;
    dsTraitEDATE: TFIBDateField;
    dsTraitVIEW_VALUE: TFIBStringField;
    dbtBdate: TDBDateTimeEditEh;
    dbtEdate: TDBDateTimeEditEh;
    Label2: TLabel;
    Label3: TLabel;
    dbeValue: TDBEditEh;
    Label4: TLabel;
    lcbValueId: TDBLookupComboboxEh;
    procedure FormShow(Sender: TObject);
    procedure dsTraitListAfterOpen(DataSet: TDataSet);
    procedure aCancelExecute(Sender: TObject);
    procedure aSaveExecute(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    PAbonent_id, PTrait_id, PAbonent2Trait_id :integer;
  end;

function ShowAbonentTraitCard(PAbonent_id, PTrait_id, PAbonent2Trait_id :integer; aOwner :TComponent): boolean;

implementation

uses uDM, u_additional;

{$R *.dfm}

function ShowAbonentTraitCard(PAbonent_id, PTrait_id, PAbonent2Trait_id :integer; aOwner :TComponent): boolean;
var
  frmAbonentTraitSet: TfrmAbonentTraitSet;
begin
  Result := False;

  try
    frmAbonentTraitSet := TfrmAbonentTraitSet.Create(aOwner);

    frmAbonentTraitSet.PAbonent_id := PAbonent_id;
    frmAbonentTraitSet.PTrait_id := PTrait_id;
    frmAbonentTraitSet.PAbonent2Trait_id := PAbonent2Trait_id;

    if frmAbonentTraitSet.ShowModal = mrOk then
      Result := True;

  finally
    FreeAndNil(frmAbonentTraitSet);
  end;

end;

procedure TfrmAbonentTraitSet.FormShow(Sender: TObject);
begin
  error_mode := te_Stop;
  dsTraitList.OpenWP([PTrait_id]);

  if PAbonent2Trait_id > 0 then begin // Edit
    dsTrait.OpenWP([PAbonent2Trait_id]);
  end
  else begin // Insert
    dsTrait.Open;
    dsTrait.Insert;
    dsTraitABONENT_ID.Value := PAbonent_id;
    dsTraitTRAITS_ID.Value := PTrait_id;
    dsTrait.Post;
  end;
end;

procedure TfrmAbonentTraitSet.dsTraitListAfterOpen(DataSet: TDataSet);
begin

  lcbValueId.Visible := dsTraitListLIST_TRAIT.AsBoolean;
  dbeValue.Visible := not dsTraitListLIST_TRAIT.AsBoolean;

end;

procedure TfrmAbonentTraitSet.aCancelExecute(Sender: TObject);
begin
  ModalResult := mrCancel;
end;

procedure TfrmAbonentTraitSet.aSaveExecute(Sender: TObject);
begin
  if dsTrait.State in [dsEdit, dsInsert] then
    dsTrait.Post;

  if trLocal.Active then
    trLocal.Commit;

  ModalResult := mrOk;

end;

end.
